<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>员工列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
<div class="container">
    <header class="header">
        <h1>员工管理系统</h1>
        <div class="user-info">
            欢迎, ${loggedInUser.username}!
            <a href="${pageContext.request.contextPath}/dashboard" class="btn btn-secondary">返回首页</a>
            <a href="${pageContext.request.contextPath}/logout" class="btn btn-secondary">退出登录</a>
        </div>
    </header>

    <main class="main-content">
        <div class="toolbar">
            <h2>员工列表</h2>
            <a href="${pageContext.request.contextPath}/employees?action=add" class="btn btn-primary">添加员工</a>
        </div>

        <!-- 搜索表单 -->
        <form class="search-form" method="get" action="${pageContext.request.contextPath}/employees">
            <input type="text" name="name" placeholder="员工姓名" value="${query.name}">
            <input type="text" name="department" placeholder="部门" value="${query.department}">
            <input type="text" name="position" placeholder="职位" value="${query.position}">
            <button type="submit" class="btn btn-primary">搜索</button>
            <a href="${pageContext.request.contextPath}/employees" class="btn btn-secondary">重置</a>
        </form>

        <!-- 员工表格 -->
        <table class="data-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>部门</th>
                <th>职位</th>
                <th>薪资</th>
                <th>入职日期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach var="employee" items="${page.data}">
                <tr>
                    <td>${employee.id}</td>
                    <td>${employee.name}</td>
                    <td>${employee.department}</td>
                    <td>${employee.position}</td>
                    <td>${employee.salary}</td>
                    <td>${employee.hireDate}</td>
                    <td class="actions">
                        <a href="${pageContext.request.contextPath}/employees?action=view&id=${employee.id}"
                           class="btn btn-info">查看</a>
                        <a href="${pageContext.request.contextPath}/employees?action=edit&id=${employee.id}"
                           class="btn btn-warning">编辑</a>
                        <form method="post" action="${pageContext.request.contextPath}/employees"
                              style="display: inline;" onsubmit="return confirm('确定删除吗？')">
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="id" value="${employee.id}">
                            <button type="submit" class="btn btn-danger">删除</button>
                        </form>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>

        <!-- 分页 -->
        <div class="pagination">
            <c:if test="${page.page > 1}">
                <a href="?page=${page.page - 1}&name=${query.name}&department=${query.department}&position=${query.position}"
                   class="btn btn-secondary">上一页</a>
            </c:if>

            <span>第 ${page.page} 页 / 共 ${page.totalPages} 页 (${page.total} 条记录)</span>

            <c:if test="${page.page < page.totalPages}">
                <a href="?page=${page.page + 1}&name=${query.name}&department=${query.department}&position=${query.position}"
                   class="btn btn-secondary">下一页</a>
            </c:if>
        </div>
    </main>
</div>
</body>
</html>